package com.test.dababy.dababy_00_utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.dababy.R;
import com.test.dababy.dababy_00_http.OkHttpUtil;

public class Photo_dababySelector_dababyDialog extends Dialog {


    private double bRqD=0.9960075142282069;
    private boolean rj9T=true;
    private String mGNC="";
    private String qFVN="";
    private double bW9r=0.7331312075386757;
    private boolean aIz3=true;
    private String vBNG="";
    private boolean pDO1=true;

    public void hB5R4(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getvBNG()))
                    setmGNC(String.valueOf(getbRqD())+isRj9T()+getqFVN()+getbW9r()+isaIz3()+ispDO1());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setbRqD(double bRqD) {
        this.bRqD = bRqD;
    }

    public void setRj9T(boolean rj9T) {
        this.rj9T = rj9T;
    }

    public void setmGNC(String mGNC) {
        this.mGNC = mGNC;
    }

    public void setqFVN(String qFVN) {
        this.qFVN = qFVN;
    }

    public void setbW9r(double bW9r) {
        this.bW9r = bW9r;
    }

    public void setaIz3(boolean aIz3) {
        this.aIz3 = aIz3;
    }

    public void setvBNG(String vBNG) {
        this.vBNG = vBNG;
    }

    public void setpDO1(boolean pDO1) {
        this.pDO1 = pDO1;
    }

    public double getbRqD() {
        return bRqD;
    }

    public boolean isRj9T() {
        return rj9T;
    }

    public String getmGNC() {
        return mGNC;
    }

    public String getqFVN() {
        return qFVN;
    }

    public double getbW9r() {
        return bW9r;
    }

    public boolean isaIz3() {
        return aIz3;
    }

    public String getvBNG() {
        return vBNG;
    }

    public boolean ispDO1() {
        return pDO1;
    }

    private final Runnable runnableByTakePhoto, runnableByFromAlbum;

    private View tpView,faView;
    private ImageView tpIv,faIv;
    private TextView tpTv,faTv;
    private ImageView tpcIv,facIv;
    private Button confirmBtn;

    private boolean is_dababyTake_dababyPhoto = true;

    private final String TAG;

    private final int colorLight,colorGray;

    public Photo_dababySelector_dababyDialog(@NonNull Context context, Runnable takeAPhotoRunnable, Runnable fromAlbumRunnable,String tag) {
        super(context);
        this.runnableByTakePhoto = takeAPhotoRunnable;
        this.runnableByFromAlbum = fromAlbumRunnable;
        this.TAG = tag;

        colorLight = context.getColor(R.color.main);
        colorGray = Color.parseColor("#FF8497B4");
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dababy_photo_selector);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.ddps_ib_close).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"CLOSE_CLICK","");
            dismiss();
        });

        tpView = findViewById(R.id.ddps_view_take_photo);
        faView = findViewById(R.id.ddps_view_from_album);
        tpIv = findViewById(R.id.ddps_iv_take_photo);
        faIv = findViewById(R.id.ddps_iv_from_album);
        tpTv = findViewById(R.id.ddps_tv_take_photo);
        faTv = findViewById(R.id.ddps_tv_from_album);
        tpcIv = findViewById(R.id.ddps_iv_take_photo_check);
        facIv = findViewById(R.id.ddps_iv_from_album_check);
        confirmBtn = findViewById(R.id.ddps_btn_confirm);

        hB5R4();

        init_dababyView();
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    private void init_dababyView(){

        tpView.setOnClickListener(v -> selectTakePhoto());
        faView.setOnClickListener(v -> selectFromAlbum());

        confirmBtn.setOnClickListener(v -> {

            OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"CONFIRM_CLICK","");

            if (is_dababyTake_dababyPhoto){
                if (runnableByTakePhoto !=null) runnableByTakePhoto.run();
                dismiss();
            }else {
                if (runnableByFromAlbum !=null) runnableByFromAlbum.run();
                dismiss();
            }
        });

    }

    private void selectTakePhoto(){
        is_dababyTake_dababyPhoto = true;

        OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"TAKE_A_PHOTO_CLICK","");

        tpView.setBackgroundResource(R.drawable.bg_blue_border_rc_12dp);
        tpIv.setBackgroundResource(R.drawable.icon_camera_light);
        tpTv.setTextColor(colorLight);
        tpcIv.setBackgroundResource(R.drawable.icon_photo_select_true);

        faView.setBackgroundResource(R.drawable.bg_trans_ll_rc16dp);
        faIv.setBackgroundResource(R.drawable.icon_album_gray);
        faTv.setTextColor(colorGray);
        facIv.setBackgroundResource(R.drawable.icon_photo_select_gray);

    }

    private void selectFromAlbum(){
        is_dababyTake_dababyPhoto = false;

        OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"UPLOAD_FROM_PHOTO_ALBUM_CLICK","");

        tpView.setBackgroundResource(R.drawable.bg_trans_ll_rc16dp);
        tpIv.setBackgroundResource(R.drawable.icon_camera_gray);
        tpTv.setTextColor(colorGray);
        tpcIv.setBackgroundResource(R.drawable.icon_photo_select_gray);

        faView.setBackgroundResource(R.drawable.bg_blue_border_rc_12dp);
        faIv.setBackgroundResource(R.drawable.icon_album_light);
        faTv.setTextColor(colorLight);
        facIv.setBackgroundResource(R.drawable.icon_photo_select_true);

    }

    @Override
    public void show() {
        super.show();
        OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"END","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"START","");
    }

}
